<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\LotteryResult;
use App\Services\RedisService;

use Log,Session;

class IndexController extends Controller
{
    public function index(Request $request)
    {    	
        $key = 'h5:gift:';
        $numKey = 'h5:gift:num:';
        
        $gift_1 = RedisService::gift_data(1);      
        $gift_2 = RedisService::gift_data(2);
        
        $gift_3 = RedisService::gift_data(3);
       
        $gift_4 = RedisService::gift_data(4);
        $gift_7 = RedisService::gift_data(7);
        //$gift_8 = RedisService::gift_data(8);
        $gift_9 = RedisService::gift_data(9);

     
        $today_people = RedisService::today_people_num();
        $total_people = LotteryResult::select('openid')->groupBy('openid')->get()->count();
        

        $gifted_today = RedisService::get_gifted_num(date('Ymd'));
        $gifted_total = RedisService::get_gifted_num();
        //dd($gifted_today);
        
        
    	return view('admin.index',compact('list','param','gift_1','gift_2','gift_3','gift_4','gift_7','gift_9','today_people','total_people','gifted_today','gifted_total'));
    }

    public function lotteryInfo($id)
    {
        $data = LotteryResult::find($id);
        return view('admin.converte',compact('data'));
    }

    /**
     * 兑奖
     */
    public function lotteryConverte(Request $request)
    {
        $rs['status'] = false;
        $rs['msg']    = '操作失败';
        $data = $request->except(['_token']);
        $data['is_converted'] = 1;
        $data['converted_at'] = date('Y-m-d H:i:s');
        $flag = LotteryResult::where('id',$data['id'])->update($data);
        if($flag) {
            $rs['status'] = true;
            $rs['msg']    = '兑奖成功';
        }
        return redirect('zadmin/')->with('rs',$rs);
    }

    private function getResultByType($type)
    {
    	$where = [];
        //$where['gift_type'] = $type;
    	return LotteryResult::where($where)
        					->count();
    }
}
